package com.xuecheng.order.utils;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayConfigImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Sober_wxy
 * @date 2022/7/26 22:46
 */
@Slf4j
@Component
public class PayUtils {

        @Autowired
        private WXPay wxPay;
        @Autowired
        private WXPayConfigImpl payConfig;

        //构造二维码--订单号，总金额，商品信息
        public String createOrder(String orderNo, Long totalPay, String desc){
                Map<String, String> data = new HashMap<>();
                // 商品描述
                data.put("body", desc);
                // 订单号
                data.put("out_trade_no", orderNo);
                //金额，单位是分
                data.put("total_fee", totalPay.toString());
                //调用微信支付的终端IP
                data.put("spbill_create_ip", "127.0.0.1");
                //回调地址
                data.put("notify_url", payConfig.getNotifyUrl());
                // 交易类型为扫码支付
                data.put("trade_type", payConfig.getPayType());

                // 利用wxPay工具,完成下单
                Map<String, String> result = null;
                try {
                        result = wxPay.unifiedOrder(data);
                } catch (Exception e) {
                        log.error("【微信下单】创建预交易订单异常失败", e);
                        throw new RuntimeException("微信下单失败", e);
                }
                // 校验业务状态
                String resultCode = result.get("result_code");
                if ("FAIL".equals(resultCode)) {
                        log.error("【微信支付】微信支付业务失败，错误码：{}，原因：{}", result.get("err_code"), result.get("err_code_des"));
                        throw new RuntimeException("【微信支付】微信支付业务失败");
                }

                // 下单成功，获取支付链接
                String url = result.get("code_url");
                if (StringUtils.isBlank(url)) {
                        throw new RuntimeException("微信下单失败，支付链接为空");
                }
                return url;
        }
        //根据订单编号查询订单
        public Boolean checkPayment(String orderNo) {
                Map map = new HashMap();
                map.put("out_trade_no",orderNo);
                try {
                        Map result = wxPay.orderQuery(map);
                        //success表示交易成功
                        return "SUCCESS".equals(result.get("trade_state"));
                } catch (Exception e) {
                        log.error("【微信查询】查询微信支付状态失败", e);
                        return false;
                }
        }

        //关闭订单
        public Boolean closeOrder(String orderNo) {
                Map map = new HashMap();
                map.put("out_trade_no",orderNo);
                try {
                        Map result = wxPay.closeOrder(map);
                        //success表示交易成功
                        String state = (String)result.get("result_code");
                        return "SUCCESS".equals(state);
                } catch (Exception e) {
                        log.error("【微信关闭】关闭微信支付状态失败", e);
                        return false;
                }
        }
}
